package com.laolang.shop.common.data.mvc.jackson;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;

/**
 * @author khlbat
 * @version 1.0
 * @date 2020/11/1 20:46
 */
@Configuration
public class JacksonConfig {

    @Bean("jackson2ObjectMapperBuilderCustomizer")
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapper() {
        return jacksonObjectMapperBuilder -> {
            SimpleModule simpleModule = new SimpleModule();
            // Long -> String
            simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
            simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
            // BigDecimal -> 0.00
            simpleModule.addSerializer(BigDecimal.class, new BigDeciamlJsonSerializer());
            jacksonObjectMapperBuilder.modulesToInstall(simpleModule);

        };
    }
}
